#
#  Projectile.py
#  TurnShip
#
#  Created by Pete Lord on 25/05/2009.
#  Copyright (c) 2009 Propellor and Ratchet Games. All rights reserved.
#
import Options

class Projectile():
    def __init__(self, entities, xy, top, target, frames):
        self.entities = entities
        self.x = xy[0]
        self.y = xy[1]
        self.target_x = self.target_location(target)[0]
        self.target_y = self.target_location(target)[1]
        self.top = top
        self.mod = 0
        self.amount = 1
        
        if self.x > self.target_x:
            self.x_speed = (self.x - self.target_x) / frames
        else:
            self.x_speed = -((self.target_x - self.x) / frames)
        if self.y > self.target_y:
            self.y_speed = (self.y - self.target_y) / frames
        else:
            self.y_speed = -((self.target_y - self.y) / frames)
    
    def poll(self):
        self.mod += self.amount
        if self.mod == self.top:
            self.amount = -self.amount
        self.x += self.x_speed
        self.y += self.y_speed
        
        
    def screen_location(self):
        # get the tuple (x, y) of the sprites screen location
        x, y = self.shadow_location()
        y += self.mod
        return (x, y - Options.TILE_HEIGHT[Options.ZOOM_LEVEL])
        
    def shadow_location(self):
        x = (self.x - self.y + self.entities.map.height - 1) * 0.5 * Options.TILE_WIDTH[Options.ZOOM_LEVEL]
        y = (self.x + self.y) * 0.5 * Options.TILE_HEIGHT[Options.ZOOM_LEVEL]
        return (x, y)
        
    def target_location(self, target):
        x = (target[0] - target[1] + self.entities.map.height - 1) * 0.5 * Options.TILE_WIDTH[Options.ZOOM_LEVEL]
        y = (target[0] + target[1]) * 0.5 * Options.TILE_HEIGHT[Options.ZOOM_LEVEL]
        return (x, y)